package com.lujianfei.netease_android_mvvm_kotlin.viewmodel

import androidx.lifecycle.MutableLiveData
import com.lujianfei.netease_android_mvvm_kotlin.App
import com.lujianfei.netease_android_mvvm_kotlin.R
import com.lujianfei.netease_android_mvvm_kotlin.adapter.MainAdapter
import com.lujianfei.netease_android_mvvm_kotlin.base.BaseViewModel
import com.lujianfei.netease_android_mvvm_kotlin.entity.DataBean

class MainViewModel : BaseViewModel() {

    private var _uiState = MutableLiveData<MainLiveData>()
    private val mDataLists = ArrayList<DataBean>()

    val uiState: MutableLiveData<MainLiveData>
        get() {
            return _uiState
        }

    data class MainLiveData(
        var datalist: MutableList<DataBean>? = null,
        var showToast: String? = null,
        var title: String? = null,
        var updateSelectAll: Boolean? = null,
        var positionToChange: Int? = null
    )

    fun clickDelete() {
        val filter = mDataLists.filter { it.checked }
        mDataLists.removeAll(filter)
        val quantity = getSelectedCount()
        emitUI(
            dataBeans = mDataLists,
            updateSelectAll = !isUnSelectAll(),
            title = App.instance.resources?.getQuantityString(
                R.plurals.selected,
                quantity,
                quantity
            )
        )
    }

    fun clickItem(data: DataBean, editMode: Boolean) {
        val position = mDataLists.indexOf(data)
        if (editMode) {
            data.checked = !data.checked
            val quantity = getSelectedCount()
            emitUI(
                updateSelectAll = !isUnSelectAll(),
                title = App.instance.resources?.getQuantityString(
                    R.plurals.selected,
                    quantity,
                    quantity
                ),
                dataBeans = mDataLists,
                positionToChange = position
            )
        } else {
            emitUI(showToast = "${data.title} 被点击")
        }
    }

    fun load() {
        for (index in 1..10) {
            mDataLists.add(
                DataBean(
                    id = index,
                    title = "title $index",
                    summary = "summary $index",
                    type = MainAdapter.TYPE_ITEM
                )
            )
        }
        mDataLists.add(
            DataBean(
                id = 11,
                title = "title",
                summary = "summary",
                type = MainAdapter.TYPE_FOOTER
            )
        )
        uiState.value = MainLiveData(mDataLists)
    }


    private fun emitUI(
        dataBeans: ArrayList<DataBean>? = null,
        showToast: String? = null,
        title: String? = null,
        updateSelectAll: Boolean? = null,
        positionToChange: Int? = null
    ) {
        uiState.value =
            MainLiveData(dataBeans, showToast, title, updateSelectAll, positionToChange)
    }

    private fun getSelectedCount(): Int {
        var selected = 0
        for (dataList in mDataLists) {
            if (dataList.checked) {
                ++selected
            }
        }
        return selected
    }

    private fun isUnSelectAll(): Boolean {
        var unselectAll = true
        for (dataList in mDataLists) {
            if (dataList.checked) {
                unselectAll = false
                break
            }
        }
        return unselectAll
    }

    fun setSelectAll(selectAll: Boolean) {
        for (dataList in mDataLists) {
            dataList.checked = selectAll
        }
        val quantity = getSelectedCount()
        emitUI(
            dataBeans = mDataLists,
            title = App.instance.resources?.getQuantityString(
                R.plurals.selected,
                quantity,
                quantity
            ),
            positionToChange = 0
        )
    }
}